package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.CreditShop;
import cn.wolfcode.trip.base.domain.ProductContent;
import cn.wolfcode.trip.base.domain.ProductPlus;
import cn.wolfcode.trip.base.mapper.CreditShopMapper;
import cn.wolfcode.trip.base.mapper.ProductContentMapper;
import cn.wolfcode.trip.base.mapper.ProductPlusMapper;
import cn.wolfcode.trip.base.query.CreditShopQuery;
import cn.wolfcode.trip.base.service.ICreditShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CreditShopServiceImpl implements ICreditShopService{
    @Autowired
    private ProductPlusMapper productPlusMapper;
    @Autowired
    private ProductContentMapper productContentMapper;
    @Autowired
    private CreditShopMapper creditShopMapper;
    @Override
    public void save(CreditShop creditShop) {
        creditShopMapper.insert(creditShop);//游记基本信息
    }

    @Override
    public void update(CreditShop creditShop) {
        creditShopMapper.updateByPrimaryKey(creditShop);
    }

    @Override
    public CreditShop get(Long id) {

        return creditShopMapper.selectByPrimaryKey(id);
    }


    @Override
    public PageInfo selectForList(CreditShopQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<CreditShop> lsit = creditShopMapper.selectForList(qo);
        return new PageInfo(lsit);
    }

    @Override
    public ProductContent getContent(Long productId) {
        return productContentMapper.selectByPrimaryKey(productId);
    }

    @Override
    public void changeState(CreditShop creditShop) {
        if (creditShop.getState() == CreditShop.STATE_SOLDOUT) {
            creditShop.setSoldoutTime(new Date());
            creditShop.setPutawayTime(null);
        } else if (creditShop.getState() == CreditShop.STATE_PUTAWAY) {
            creditShop.setSoldoutTime(null);
            creditShop.setPutawayTime(new Date());
        }
        creditShopMapper.changeState(creditShop);
    }

    @Override
    public void saveOrUpdate(CreditShop creditShop) {
        ProductContent content = new ProductContent();
        ProductPlus productPlus = new ProductPlus();
        productPlus.setSale(0L);
        creditShop.setState(CreditShop.STATE_WAIT);
        creditShop.setCreateTime(new Date());
        creditShop.setIsRecommend(0);
        if (creditShop.getId() != null) {
            creditShopMapper.updateByPrimaryKey(creditShop);
            Long productId = creditShop.getId();
            content.setId(productId);
            content.setContent(creditShop.getContent().getContent());
            productContentMapper.updateByPrimaryKey(content);
            productPlus.setId(productId);
            productPlus.setCount(creditShop.getProductPlus().getCount());
            productPlusMapper.updateByPrimaryKey(productPlus);
        } else {
            creditShopMapper.insert(creditShop);
            Long productId = creditShop.getId();
            content.setId(productId);
            content.setContent(creditShop.getContent().getContent());
            productContentMapper.insert(content);
            productPlus.setId(productId);
            productPlus.setCount(creditShop.getProductPlus().getCount());
            productPlusMapper.insert(productPlus);
        }

    }

    @Override
    public void addCount(Long productId, Long count) {
        productPlusMapper.updateCountByProductId(productId, count);
    }

    @Override
    public void changeCommend(CreditShop creditShop) {
        creditShopMapper.changeCommend(creditShop);
    }

    @Override
    public PageInfo getProductTypes(CreditShopQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<CreditShop> lsit = creditShopMapper.getProductTypes(qo);
        return new PageInfo(lsit);
    }

    @Override
    public void changeCount(Long productId, Long count) {
        productPlusMapper.changeCount(productId, count);
    }
}
